<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Controller/Action.php';
class Admin_SettingController extends Sunnet_Controller_Action
{
    var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (!$this->_arrParam['page']) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (!$this->_arrParam['line_per_page']) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
         Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
    }

    public function preDispatch(){
		$template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');
//    	$auth = Zend_Auth::getInstance();
//    	if (!$auth->hasIdentity()) {
//    		$this->_forward('index','login','admin');//,$this->_arrParam);
//    	}
    }
    function indexAction(){
        
    }
    function listMenuAction(){
        Zend_Loader::loadClass('Admin_Models_AdminMenuModel');
        $db_menu = new Admin_Models_AdminMenuModel();
        $mainmenu =$db_menu->loadmainmenu();
        $this->view->mainmenu = $mainmenu;
        $id=$this->_request->getParam('del_menu');
        if(isset($id)){
            $db_menu->delete('id ='.$id);
            echo "<script>alert('Menu deleted');window.location = '/admin/setting/list-menu';</script>";
        }
    }
    function addMenuAction(){
       
        Zend_Loader::loadClass('Admin_Models_AdminMenuModel');
        Zend_Loader::loadClass('Admin_Forms_AddMenuForm');
        $db_menu = new Admin_Models_AdminMenuModel();
        $mainmenu =$db_menu->loadmainmenu();
        $form = new Admin_Forms_AddMenuForm($mainmenu);
        $this->view->form = $form;
        $request=$this->getRequest();
				
		if($request->isPost()){
		
				
                
				$validator= $this->_request->getParams();
                                if ($form->isValid($validator)==FALSE){
						$this->view->messagesError = $form->getMessagesError();
				}else {
                                try {
                                                            
                                        
                                        $data_menu = array(
                                            'name'=>$this->_request->getParam('name'),
                                            'link'=>$this->_request->getParam('link'),
                                            'parent'=>$this->_request->getParam('menulist'),
                                            'ordering'=>$this->_request->getParam('ordering'),
                                            'controller'=>$this->_request->getParam('controller_add'),
                                            
                                            
                                        );
                                       
                                       $result= $db_menu->insert($data_menu);
                                        if($result>0)
					{
						echo "<script>alert('Menu added');window.location = '/admin/setting/list-menu';</script>";
						
					}else {
						echo "<script>alert('Add menu Fail');window.location = '/admin/setting/list-menu/';</script>";
					}
                                }catch(Exception $e){
                                   
                                }
                }
          }
    }
}
?>
